#include "../include/login.h"
#include "../include/password.h"
#define add_div "add"//order:增加设备
#define add_div "add"//order:增加设备

extern TRAN m_node;//使用其他.c文件中的全局变量
char dev_buff[1024]={0};
int device_system(int client_socket){
	int ch = 0;
	TRAN node;
	DTRAN dnode;//存放新增设备信息
	char t_val[10];
	while(ch!=-1){
		bzero(&dnode,sizeof(dnode));
		bzero(&node,sizeof(node));
		//选择菜单
		printf("请选择设备操作:\n1:数据监测\n2:设备预警值修改:\n3:退出");
		if(m_node.perm==1){
			printf("3:新增监测设备\n");
			printf("4:撤销监测设备\n");
		}
		printf("其他任意键退出\n");
		scanf("%d",&ch);
		if(ch!=1 && ch!=2){//退出
			break;
		}
		if(m_node.perm==1 && ch!=3 && ch!=4){
			break;
		}
		switch(ch){
			case 1://数据监测
				dev_json_printf();
				//待添加解析JSON数组功能
				break;
			case 2://
				break;
			case 3://增加设备
			/*
				char dev_order[20];//信息用途 比如登录,注册...
				char dev_name[20];//设备名
				char dev_item[20];//监测项目
				//char dev_res[20];//监测数据
				char dev_t_val[20];//阈值
				//char dev_mytime[128];//时间戳
			*/
				strcpy(dnode.dev_order,add_div);
				puts("请输入设备名称");
				scanf("%s",dev_name);
				puts("请输入设备监测项目");
				scanf("%s",dev_item);
				while(1){
					puts("请输入阈值最小值('null'为不设最小值)");
					scanf("%s",dev_t_val);
					//辨别输入的字符串是否能转化为数字
					if(strcmp(dev_t_val,"null")!=0){
						char *endptr;
						strtod(dev_t_val, &endptr);
						// Check for errors
						if(endptr != str) {
							printf("含非数字字符,请重新输入\n");
							continue;
						}
					}
					break;
				}
				strcat(dev_t_val,"-")
				while(1){
					puts("请输入阈值最大值('nul'为不设最大值)");
					scanf("%s",t_val);
					//辨别输入的字符串是否能转化为数字
					if(strcmp(dev_t_val,"null")!=0){
						char *endptr;
						strtod(dev_t_val, &endptr);
						// Check for errors
						if(endptr != str) {
							printf("含非数字字符,请重新输入\n");
							continue;
						}
					}
					break;
				}
				strcat(dev_t_val,val);
				dev_json_add(&dnode);//添加设备进数据库
				write(client_socket,dev_buff,sizeof(dev_buff));
				
				puts("准备从服务器接收返回值");
				bzero(buff,sizeof(buff));
				sleep(2);
				int num=read(client_socket,buff,1024);
				if(num<=0){
					puts("已超时,未能接收到返回值");
					return -1;
				}
				else
					printf("接收到返回值:%s\n",buff);
				break;
			case 4://删除设备
				break;
		}
	}
	return 0;	
}

void dev_json_printf(void)
{
	cJSON* root = cJSON_CreateObject();
	cJSON* order = cJSON_CreateString("look");
	cJSON_AddItemToObject(root,"order",order);
	char *p=dev_buff;
	p=cJSON_Print(root);
	strcpy(dev_buff,p);
	cJSON_Delete(root);
}

void dev_json_add(DTRAN *node)
{
	cJSON* root = cJSON_CreateObject();//创建
	
	cJSON* order = cJSON_CreateString(node->dev_order);
	cJSON* dev_name = cJSON_CreateString(node->dev_name);
	cJSON* item = cJSON_CreateString(node->dev_item);
	cJSON* t_val = cJSON_CreateString(node->dev_t_val);
	cJSON* newpas = cJSON_CreateString("");

	cJSON_AddItemToObject(root,"order",order);
	cJSON_AddItemToObject(root,"dev_name",dev_name);
	cJSON_AddItemToObject(root,"item",item);
	cJSON_AddItemToObject(root,"t_val",t_val);
	cJSON_AddItemToObject(root,"newpas",newpas);
	
	char *p=pas_buff;
	p=cJSON_Print(root);
	strcpy(pas_buff,p);
	cJSON_Delete(root);
}